//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.BaseDao;
import com.aidong.model.wills.PssProductStock;
import com.aidong.model.wills.PssProductStockExt;
import com.aidong.model.wills.PssProductStockForTotal;
import com.aidong.model.wills.PssProductStockPK;
import java.util.ArrayList;
import org.springframework.stereotype.Repository;

@Repository
public class PssProductStockDao extends BaseDao<PssProductStock, PssProductStockPK> {
    public PssProductStockDao() {
    }

    public PssProductStock findProductStock(String shopCode, String productCd) {
        return (PssProductStock)this.getByWhere("ShopCode=:p1 and ProductCode=:p2", new Object[]{shopCode, productCd});
    }

    public void getProductStockLst(Pagination<PssProductStockExt> pagination, ConditionMap cd) {
        ArrayList params = new ArrayList();
        String sqlw = cd.toSql(params);
        String sql = " select a.ShopCode,a.ProductCode,a.Quantity,b.PurcPrice as PurcPrice1,a.Quantity*b.PurcPrice as Amount1,b.Unit \n,isnull((select sum(Amount)/sum(Quantity) from PssPurcReceiveList where ProductCode=a.ProductCode and Quantity>0  \ngroup by ProductCode having sum(Quantity)>0),0) as PurcPrice2 \nfrom PssProductStock a \ninner join PssProductCode b on a.ProductCode = b.ProductCode and b.Status = 1 \n" + (sqlw.length() > 0?"where " + sqlw:"");
        sql = sql + "  order by a.ShopCode,a.ProductCode \n";
        this.queryPageListBySql(PssProductStockExt.class, pagination, sql, params.toArray());
    }

    public void getProductPssLstByShopCode(Pagination<PssProductStockForTotal> pagination, ConditionMap cd, String dateFrom, String dateTo) {
        ArrayList params = new ArrayList();
        params.add(dateFrom);
        params.add(dateTo);
        params.add(dateFrom);
        params.add(dateTo);
        params.add(dateFrom);
        params.add(dateTo);
        params.add(dateFrom);
        params.add(dateTo);
        params.add(dateFrom);
        params.add(dateTo);
        params.add(dateFrom);
        params.add(dateTo);
        params.add(dateFrom);
        params.add(dateTo);
        String sqlw = cd.toSql(params);
        String sql = " select a.ShopCode \n,a.ProductCode \n,b.Unit \n,a.Quantity \n,isnull(c.cgQty1, 0) as cgQty1 \n,isnull(d.cgQty2, 0) as cgQty2 \n,isnull(e.psQty1, 0) as psQty1 \n,isnull(f.psQty2, 0) as psQty2 \n,isnull(g.xsQty1, 0) as xsQty1 \n,isnull(h.bsQty1, 0) as bsQty1 \n,isnull(i.lyQty1, 0) as lyQty1 \nfrom PssProductStock a \ninner join PssProductCode b on a.ProductCode = b.ProductCode and b.Status = 1 \nleft join (select ppr.ShopCode, pprl.ProductCode, sum(pprl.Quantity) as cgQty1 from PssPurcReceive ppr inner join PssPurcReceiveList pprl on ppr.ReceiveID=pprl.ReceiveID \nwhere ppr.ReceiveDate >= :p1 \nand ppr.ReceiveDate <= :p2 \nand ppr.Status = 1 \ngroup by ppr.ShopCode, pprl.ProductCode) c on a.ShopCode = c.ShopCode and a.ProductCode = c.ProductCode \nleft join (select ppr.ShopCode, pprl.ProductCode, sum(pprl.Quantity) as cgQty2 from PssPurcReturn ppr inner join PssPurcReturnList pprl on ppr.ReturnID=pprl.ReturnID \nwhere ppr.ReturnDate >= :p3 \nand ppr.ReturnDate <= :p4 \ngroup by ppr.ShopCode, pprl.ProductCode) d on a.ShopCode = d.ShopCode and a.ProductCode = d.ProductCode \nleft join (select ppr.ReceiveShop, pprl.ProductCode, sum(pprl.Quantity) as psQty1 from PssTranSend ppr inner join PssTranReceive pprl on ppr.SendId=pprl.SendId \nwhere pprl.ReceiveDate >= :p5 \nand pprl.ReceiveDate <= :p6 \ngroup by ppr.ReceiveShop, pprl.ProductCode) e on a.ShopCode = e.ReceiveShop and a.ProductCode = e.ProductCode \nleft join (select ppr.SendShop, pprl.ProductCode, sum(pprl.Quantity) as psQty2 from PssTranSend ppr inner join PssTranSendlist pprl on ppr.SendId=pprl.SendId \nwhere ppr.SendDate >= :p7 \nand ppr.SendDate <= :p8 \ngroup by ppr.SendShop, pprl.ProductCode) f on a.ShopCode = f.SendShop and a.ProductCode = f.ProductCode \nleft join (select ppr.ShopCode, pprl.ProductCode, sum(pprl.Quantity) as xsQty1 from PssProductSale ppr inner join PssProductSaleList pprl on ppr.AutoId=pprl.ProductSaleId \nwhere ppr.SaleDate >= :p9 \nand ppr.SaleDate <= :p10 \ngroup by ppr.ShopCode, pprl.ProductCode) g on a.ShopCode = g.ShopCode and a.ProductCode = g.ProductCode \nleft join (select ppr.LossShop, pprl.ProductCode, sum(pprl.Quantity) as bsQty1 from PssLossOrder ppr inner join PssLossOrderList pprl on ppr.LossId=pprl.LossId \nwhere ppr.LossDate >= :p11 \nand ppr.LossDate <= :p12 \ngroup by ppr.LossShop, pprl.ProductCode) h on a.ShopCode = h.LossShop and a.ProductCode = h.ProductCode \nleft join (select ppr.UseShop, pprl.ProductCode, sum(pprl.Quantity) as lyQty1 from PssUseOrder ppr inner join PssUseOrderList pprl on ppr.UseId=pprl.UseId \nwhere ppr.UseDate >= :p13 \nand ppr.UseDate <= :p14 \ngroup by ppr.UseShop, pprl.ProductCode) i on a.ShopCode = i.UseShop and a.ProductCode = i.ProductCode \n" + (sqlw.length() > 0?"where " + sqlw:"");
        sql = sql + " order by a.ShopCode,a.ProductCode \n";
        this.queryPageListBySql(PssProductStockForTotal.class, pagination, sql, params.toArray());
    }
}
